package indi.zn.commons.easiness;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class FileHelper {

	 public static final byte[] JPEG_begin=new byte[]{(byte) 0xff, (byte) 0xd8};
	 public static final byte[] JPEG_end=new byte[]{ (byte) 0xff, (byte) 0xd9};
	 public static final byte[] TGA_begin=new byte[]{00,00,02,00,00};
	 public static final byte[] TGA_RLE_begin=new byte[]{00,00,10,00,00};
	 public static final byte[] PNG_begin=new byte[]{ (byte) 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A};
	 public static final byte[] GIF_begin1=new byte[]{ 0x47,0x49,0x46,0x38,0x39,0x61};
	 public static final byte[] GIF_begin2=new byte[]{ 0x47,0x49,0x46,0x37,0x39,0x61};
	 public static final byte[] BMP_begin=new byte[]{0x42,0x4D};
	 public static final byte[] TIFF_begin1=new byte[]{0x4D,0x4D};
	 public static final byte[] TIFF_begin2=new byte[]{0x49,0x49};
	 
	 public static final byte[] JAVACLASS_begin=new byte[]{(byte) 0xCA,(byte) 0xFE,(byte) 0xBA,(byte) 0xBE};
	 
	 
	public static byte[][] type=new byte[][]{JPEG_begin,TGA_begin,TGA_RLE_begin,PNG_begin,
		 GIF_begin1,GIF_begin2,BMP_begin,TIFF_begin1,TIFF_begin2,JAVACLASS_begin};
	 
	public static String[] type_suffix=new String[]{"JPG","TGA","TGA","PNG","GIF","GIF","BMP","TIFF","TIFF","CLASS"}; 
	
	public static Pattern pattern=Pattern.compile("\\w*\\(\\d\\)$"); 
	
	public static Pattern pattern2=Pattern.compile("\\(\\d\\)$"); 
	
	 
	 public static String getSuffix(File file){
		 
		 String name=file.getName();
		 
		  int d=name.lastIndexOf(".");
			
		   String suffix = name.substring(d+1, name.length());
		return suffix;
		  
	 }
	 
    public static String getSuffix(String name){
		 
		 int d=name.lastIndexOf(".");	
		 String suffix = name.substring(d+1, name.length());
		return suffix;
		  
	 }
    
    
    /**
     * 根据文件头  8 bit 尝试 识别文件类型 
     * @param file
     * @return
     * @throws IOException
     */
    public  static String fileDiscern(File file) throws IOException{
    	
    	String filetype=null;
    	FileInputStream fis=new FileInputStream(file);
    	
    	byte[] bs=new byte[8];
    	
    	  fis.read(bs);
    	
    	  fis.close();
    	  
    	  boolean flag;
    	  for (int i = 0; i < type.length; i++) {
    		   flag=true;
			
    		  for (int j = 0; j < type[i].length; j++) {
    			    
    			  if(type[i][j]==bs[j]){
    				  
    			  }else{
    				  flag=false;
    				  break;
    			  }

    		  }
    		  
    		  if(flag){
    			  
    			  filetype=type_suffix[i];
    			 
    			  break;
    		  }
		}
		return filetype;
    	  
    }  
    
    
	public static String fileReName(String dirpath,String filename) {

		File file = new File(dirpath+filename);

		if (file.exists()) {


				int d = filename.lastIndexOf(".");

				String prefix = filename.substring(0, d);

				Matcher matcher = pattern.matcher(prefix);
				
				System.out.println(prefix);
				

				if (matcher.find()) {

					String mstr = matcher.group();
					
					System.out.println(mstr);
					
				   String sn=pattern2.matcher(mstr).group();
					
					sn=sn.substring(1);
					
					sn=sn.substring(0, sn.length()-1);

					Integer in = Integer.valueOf(sn);

					sn=matcher.replaceAll("(" + (in + 1)+")");

					
					prefix=mstr.substring(0, mstr.length()-sn.length())+sn;
					
					
				} else {
					
					prefix=prefix+"(1)";

				}
				
				
				String nname=prefix+"."+getSuffix(filename);
			
				if(new File(dirpath+nname).exists()) {
					
					nname= fileReName(dirpath,nname);
				}	
				return nname;
			
		}

		return filename;

	}
    
    
    
    
    
    
    
    public static String fileSuffixReName(File file){
		return null;
    	
    }
    
    
    
    /**
     * 读取文件文本
     * @param file
     * @return
     */
    public String readFileString(File file) {
    	
    	StringBuilder result = new StringBuilder();
    	try{
    	    BufferedReader br = new BufferedReader(new FileReader(file));
    	    char[] cbuf =new char[4096] ;
    	    while(br.read(cbuf)!=-1){//使用readLine方法，一次读一行
    	        result.append(cbuf);
    	    }
    	    br.close();    
    	}catch(Exception e){
    	    e.printStackTrace();
    	}
		return result.toString();
    	
    }
    
    
    public static boolean  isDirAnMk(String dirpath) {
    	
    	File dir=new File(dirpath);
		
		if(dir.exists()){
			
			if(dir.isDirectory()){
				
				dir.mkdirs();	

			}
			
		}else {
			dir.mkdirs();			
		}
		return false;
    	
    }
    
    
    
    
    
	
    
    public static void main(String[] args) throws IOException {

//    	Matcher m= pattern2.matcher("file(1)");
//   
//    	
//    	System.out.println(m.find());
//    	
//    	System.out.println(m.group());  
    	
    	 Pattern pattern = Pattern.compile("(?<=\\()(\\d+)(?=\\))");   //[\((][^\))]+[\))]$
    	  
    	  Matcher matcher = pattern.matcher("(1)file(2)(3)");
    
    	  int i=0;
    
    	  while(matcher.find())
    	  {
    		  
    	   System.out.println(matcher.group());
    	   
    i++;
    	   
    	  }
    	  
    	  matcher.find(i);
    	  
    	  String n= matcher.group();
    	  
    	  Integer in=Integer.valueOf(n);
    	    
    	  
    	String res = Regulars.replaceMatchIndex(matcher, 3,"9");
           
      
   	   System.out.println(res);
    	   
    		
	}
    
    
}
